<?php 

/* DATABASE CONNECTION CLASS 
* by : Fernando Ribeiro <r.fernando@netc.pt>
*/

class bd{

	var $conn;
	var $dbase;
	var $_database;
	var $_user;
	var $_pass;
	var $_server;
	var $ep;	/* define entry point */
	var $confile; /* configuration file */
	
		function ligarBD($_database,$_user,$_pass,$_server){
			
			$this->$_database=$_database;
			$this->$_user=$_user;
			$this->$_pass=$_pass;
			$this->$_server=$_server;
			
			$this->ep = "func";
			
			$this->connect();
		}
		
		function connect(){
			
			$this->confile = "db.conf";
			
			if ($this->ep != "func") $this->readconf($this->confile);
			
			if (!@$this->conn = mysql_connect($this->_server,$this->_user,$this->_pass)) die ("I'm Sorry the MySQL Server could not be reached !");
			
			if ($this->conn<0) {
				return -1;
			}
			
			$this->dbase=$this->_database;
			if (mysql_select_db($this->dbase,$this->conn)==false){
			$this->error("ERROR TRYING TO IDENTIFY DATABASE, mysql_select_db(netcrash)");
				return -1 ;
			}
		}
	/* read the configuration file */
	/* style conf like:
	*  server=localhost
	*  user=Username
	*  pass=password
	*  database=mydatabase
	*/
	function readconf($file){
		if (!file_exists($file) || !is_readable($file)) die ("Error reading configuration file. ");

		$def = file($file);
		$SIZEDEF = sizeof($def);
		for ($i=0 ; $i < $SIZEDEF ; $i++){
		        $this->parse($def[$i]);
		}
	
	}
	/* parse the line of configuration file */
	function parse($line){
		if (substr_count($line,"=") == 1 ){
			$def = explode("=",$line);
			$var   = trim($def[0]); /* name of variable */
			$value = trim($def[1]); /* value of variable */
			
			switch ($var){
			case 'server':	$this->_server = $value ;
				break;
			case 'user' : $this->_user = $value;
				break;
			case 'pass' : $this->_pass = $value;
				break;
			case 'database': $this->_database = $value;
				break;
			}
		}
	}
	
	function error($function){
		echo "<hr size='1' width='50%'>";
			echo "<br>Error in function $function .<br>";
			echo "Description...<br>";
			echo "Error number ... ".mysql_errno()."<br>";
			echo mysql_error($this->conn)."<br>";
	}

	function parseResultObj($resultado){
		$n_rows= mysql_num_rows($resultado);
	   if ($n_rows  > 0 ){
		unset($entidades);
		/* Inicializa o array $entidades */
		for($i=0; $i< $n_rows ; $i++)
		{
		$entidades[$i]=mysql_fetch_Object($resultado);
		}
		unset($resultado);
		return $entidades;
		/* Retorna o array $entidades */
	   }
	}
		
	function execSQL($sql_command) {
	//echo $sql_command."<br>";
	    if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
			//echo $resultado."<br>";
			return ($this->parseResultObj($resultado));
		
		}else{
			$this->error("executarSQL()");
			return false;
		}
	}
	
	function exec($sql_command) {
	  //echo $sql_command."<br>";//.$this->dbase."- >".$this->conn."<br>";  
	    if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
			return $resultado;
		
		}else{
			$this->error("executar()");
			return false;
		}
	}
	
	/* remove last caracter from string ex: the "," */
	function rm_last($string){
	$num = strlen($string);
	$string = substr_replace($string,"",($num-1),($num+1));
	return $string;
	}
	
	/*
	* insert's into fields array( "field" => specific 
	* value $value ); in the form of an array 
	*/
	function mysql_insert($table,$array){
        $table = trim($table);

		if ($table == '') die ("Erro table not defined");
		foreach ( $array as $key => $value)
		{
			$campos.=$key.",";
			if (!is_int($value)){
				$value='"'.$value.'"';
			}
			
			$valores.=$value.",";
		}
	
		$campos = substr($campos,0,-1);
	
		$valores = substr($valores,0,-1);
	
		$sql="INSERT INTO $table ($campos) VALUES ($valores)";
		//echo "<br>".$sql."<br>";	
		return $this->exec($sql);
	
	}
	
	function mysql_update($table,$array,$condition){
        $table = trim($table);
	
	if ($table == '' || $condition == '' ) $this->error("Erro table or condition not defenide,mysql_update()");
	
	foreach ( $array as $key => $value)
	{
		if (!is_int($value)){
		 	$value='"'.$value.'"';
		 }
			$campos.=$key."=".$value.",";
	}
	$campos = substr($campos,0,-1);
		
	$sql="UPDATE $table SET $campos WHERE $condition ";
	
		return $this->exec($sql);
	}
	
	function GetLastInsertID(){
		return $used_id = mysql_insert_id($this->conn);
	} 
	
	/* To close the connection to the db */
	function fin(){
		mysql_close($this->conn);
	}


}

?>